package com.mf.ergate.web.controller.sys;

import com.mf.ergate.api.sys.model.dto.FileStorageInfoQueryDto;
import com.mf.ergate.api.sys.model.vo.FileStorageInfoVo;
import com.mf.ergate.web.common.utils.MfParamUtils;
import com.mf.ergate.web.service.sys.FileStorageInfoService;
import com.mf.framework.common.model.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 文件存储信息Controller
 * User: zhaoming
 * DateTime: 2019-03-11
 * To change this template use File | Settings | File Templates.
 */
@Secured({"PUBLIC"})
@Controller
@RequestMapping("/web/fileStorageInfo")
public class FileStorageInfoController{

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private FileStorageInfoService fileStorageInfoService;

    /**
     * 首页
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("file_storage_info_index");
        return modelAndView;
    }

    /**
     * 根据业务主键查询
     * @param uniqueNo 唯一编号
     * @return 文件存储信息
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public FileStorageInfoVo detail(String uniqueNo) {
        return fileStorageInfoService.selectByUniqueNo(uniqueNo);
    }

    /**
     * 根据条件分页查询
     * @param fileStorageInfoQueryDto 文件存储信息查询
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 文件存储信息分页列表
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<FileStorageInfoVo> page(FileStorageInfoQueryDto fileStorageInfoQueryDto,
                                    @RequestParam(value = "page") int pageNum,
                                    @RequestParam(value = "rows") int pageSize,
                                    @RequestParam(value = "sidx") String sortName,
                                    @RequestParam(value = "sord") String sortOrder) {

        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return fileStorageInfoService.selectPage(page, fileStorageInfoQueryDto);
    }

    /**
     * 下载
     * @param uniqueNo 唯一编号
     * @return 文件
     */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public ResponseEntity<byte[]> download(String uniqueNo){
        FileStorageInfoVo fileStorageInfoVo = fileStorageInfoService.selectByUniqueNo(uniqueNo);
        byte[] bytes = fileStorageInfoService.download(uniqueNo);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", fileStorageInfoVo.getFileName()));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(bytes.length)
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(bytes);
    }

}